<?php

require_once __DIR__."/../../library/jsonrpc/JsonRpc/Clients/RpcClient.php";

/**
 * Class task
 * 基于 workermanJsonRpc 的二次封装
 */
class task
{
    protected static $config;
    protected static $rpc;


    /**
     * 检查是否有配置列表
     */
    public static function check(){
        if (empty(self::$config) || empty(self::$rpc)){
            return [
                "code" => 101,
                "msg"  => "没有进行初始化配置"
            ];
        }
    }

    /**
     * @param string $class
     * @param array $config
     * 设置执行列表
     * 初始化设置
     */
    public static function set_config($class,$config = []){
        if (empty($config)){
            $config = rpc_list;
        }
        self::$config = $config;
        RpcClient::config($config);
        $rpc_client =  RpcClient::instance($class);
        self::$rpc  =  $rpc_client;
    }




    /**
     * @param $function
     * @param array $params
     * @return mixed
     * 同步调用数据
     */
    public static function sync_task($function,$params = []){
        self::check();
        $rpc_client = self::$rpc;
        $res = $rpc_client->$function($params);
        return $res;
    }


    /**
     * @param $function
     * @param array $params
     * 异步执行调用数据
     */
    public static function asend_task($function,$params = []){
        self::check();
        $rpc_client = self::$rpc;
        $function   = "asend_{$function}";
        $rpc_client->$function($params);
    }


    /**
     * @param $function
     * @param array $params
     * @return mixed
     * 获取异步调用的结果
     */
    public static function arecv_task($function,$params = []){
        self::check();
        $rpc_client = self::$rpc;
        $function   = "arecv_{$function}";
        $res        =  $rpc_client->$function($params);
        return $res;
    }



}